package com.example.geguo.deeplinkdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
/**
 * Handler 是Activity的一个内部类
 * */
public class HandlerActivity extends Activity {
	public static void startActivity(Context context){
			Intent intent = new Intent(context,HandlerActivity.class);
			context.startActivity(intent);
		}
	private final Handler mLeakyHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// ...
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_handler);
		System.out.println("线程名称："+Thread.currentThread().getName());
		// 1. 在主线程进行延时
		mLeakyHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
//				...
			}
		}, 1000 * 60 * 10);
		// 2. 在主线程 做循环
/*		mLeakyHandler.post(new Runnable() {
			@Override
			public void run() {
				try {
					//在主线程
					for (int i = 0; i < 5; i++) {
						System.out.println(Thread.currentThread().getName()+":循环"+i);
						Thread.sleep(1000);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
					System.out.println("中断异常");
				}
			}
		});*/
		// 3. 不使用handler
/*		new Thread(new Runnable() {
			@Override
			public void run() {
			   // 子线程循环5次
				for (int i = 0; i < 5; i++) {
						System.out.println(Thread.currentThread().getName()+":循环"+i);
						Thread.sleep(1000);
					}
			}
		}).start();*/
		// Go back to the previous Activity.
		finish();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.out.println(this.getClass().getSimpleName() + ":onDestroy");
	}

//	强制回收该方法不会执行哦
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		System.out.println(this.getClass().getSimpleName() + ":finalize");
	}
}
